#include "comm.hpp"

Init init;
int main()
{
    //1.通过ftok获取相同的唯一的key值
    key_t k = ftok(PATH_NAME,PROJ_ID);
    assert(k != -1);
    //2.用这个key值创建一个system V共享内存
    int shmid = shmget(k,SIZE,IPC_CREAT | IPC_EXCL | 0666);
    if(shmid == -1)
    {
        perror("shmget");
        exit(1);
    }
    //3.创建共享内存完成后，还需要将共享内存挂接到自己的地址空间
    char* shmaddr = (char *)shmat(shmid,nullptr,0);
    assert(shmaddr != nullptr);
    int fd = OpenFIFO(O_RDONLY);
    cout<<"sever打开fifo成功"<<endl;
    //4.通信的过程
    for(;;)
    {
        Wait(fd);
        printf("%s\n",shmaddr);
        if(strcmp(shmaddr,"quit") == 0)break;
    }

    //5.不用了之后就取消挂接
    int n = shmdt(shmaddr);
    assert(n != -1);
    (void)n;
    
    n = shmctl(shmid,IPC_RMID,nullptr);
    assert(n != -1);
    (void)n;
    return 0;
}